package spring.cloud.tasks.tasks_assign_manager.schedule;


import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import spring.cloud.tasks.common.node.TaskConfigurationNode;
import spring.cloud.tasks.common.node.TaskNodePath;


@Slf4j
@Setter
@Getter
public class TaskConfiguration {
    private final String taskId;

    private String taskType;
    private String linkId;//link id针对同一个task的bean id需要唯一
    private boolean enabled = true;
    private String description = "";
    //
    /**
     * 任务启动时间的cron表达式.
     */
    private String cronExpressionUpdateId;
    private String cronExpression;
    /**
     * 任务暂停时间段，日期段。
     */
    private String pauseDatePeriod = "";
    /**
     * 任务暂停时间段，小时分钟段。
     */
    private String pauseTimePeriod = "";
    /**
     * 提前进行预分配时间:单位分钟
     */
    private Integer preAssignMinutes;
    //
    /**
     * 任务分片总数.
     */
    private int taskItemTotalCount;
    /**
     * 可能被propertiesString里面的属性值替换
     */
    private String taskItemIndexAndTaskItemParametersString = "";
    private String propertiesString = "";
    //
    /**
     * 每个任务的预分配列表
     */
    private String preferExecutorIdList = "";
    /**
     * 是否使用非preferList
     */
    private boolean useDisExecutorIdList = true;
    //
    /**
     * 每个分片的权重
     */
    private Integer loadLevel = 1;


    /**
     * 任务允许延迟执行的秒数
     */
    private Integer secondAfterTaskStopCommandInstructed;
    /**
     * Job超时时间
     */
    private int timeoutMinutes;

    public TaskConfiguration(String taskId) {
        this.taskId = taskId;
    }

    public String getCronExpression(CuratorFramework curatorFramework) throws Exception {
        cronExpression = String.valueOf(curatorFramework.getData().forPath(TaskNodePath.getTaskConfigurationNodePath(taskId, TaskConfigurationNode.cron_expression)));
        return cronExpression;
    }

    public String getPauseDatePeriod(CuratorFramework curatorFramework) throws Exception {
        pauseDatePeriod = String.valueOf(curatorFramework.getData().forPath(TaskNodePath.getTaskConfigurationNodePath(taskId, TaskConfigurationNode.pause_period_date)));
        return pauseDatePeriod;
    }

    public String getPauseTimePeriod(CuratorFramework curatorFramework) throws Exception {
        pauseTimePeriod = String.valueOf(curatorFramework.getData().forPath(TaskNodePath.getTaskConfigurationNodePath(taskId, TaskConfigurationNode.pause_period_time)));
        return pauseTimePeriod;
    }

    public Integer getPreAssignMinutes(CuratorFramework curatorFramework) throws Exception {
        preAssignMinutes = Integer.parseInt(String.valueOf(curatorFramework.getData().forPath(TaskNodePath.getTaskConfigurationNodePath(taskId, TaskConfigurationNode.pre_assign_minutes))));
        return preAssignMinutes;
    }


}